package com.cxl.demo.controller;

import com.cxl.demo.mapper.RecordMapper;
import com.cxl.demo.service.RecordService;
import com.cxl.demo.vo.Record;
import com.cxl.demo.vo.ReturnData;
import com.cxl.demo.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/record")
@CrossOrigin
public class RecordController {

    @Autowired
    RecordMapper recordMapper;
    @Autowired
    ReturnData returnData;
    @Autowired
    RecordService recordService;
    @Autowired
    User user;


    @PostMapping("AddRecord")
    public ReturnData AddRecord(@RequestBody  Map<String,Object> map){
        returnData.setData(null);

        Integer ok=recordService.addRecord(user.getUserId(),(Integer) map.get("assigmentId"));
        if(ok==1){
            returnData.setStatus(true);
            returnData.setMsg("接受任务成功");
        }else{
            returnData.setStatus(false);
            returnData.setMsg("接受任务失败,任务已满。");
        }
        return returnData;
    }
    @PostMapping("AssigmentGetRecordList")
    public ReturnData AssigmentGetRecordList(@RequestBody Map<String,Object> map){
        returnData.setData(null);


        map.put("page",((Integer)map.get("currentPage")-1)*(Integer)map.get("pageSize"));
        map.put("size",(Integer)map.get("pageSize"));


        returnData.setMsg("查询完毕");
        returnData.setStatus(true);
        returnData.setData(recordService.AssigmentGetRecordList(map));
        return returnData;
    }
    @PostMapping("UserGetRecordList")
    public ReturnData UserGetRecordList(@RequestBody Map<String,Object> map){
        returnData.setData(null);
        System.out.println(map);
        map.put("page",((Integer)map.get("currentPage")-1)*(Integer)map.get("pageSize"));
        map.put("size",(Integer)map.get("pageSize"));
        map.put("userId",user.getUserId());
        returnData.setMsg("查询完毕");
        returnData.setStatus(true);
        returnData.setData(recordService.UserGetRecordList(map));
        return returnData;
    }


    @PostMapping("UpdateRecordGrade")
    public ReturnData UpdateRecordGrade(@RequestBody Record record){
        returnData.setData(null);

        Integer ok=recordService.updateRecordGrade(record);
        if(ok==1){
            returnData.setStatus(true);
            returnData.setMsg("评价成功");
        }else{
            returnData.setStatus(false);
            returnData.setMsg("评价失败");
        }

        return returnData;
    }

    @PostMapping("UpdateRecordCondition")
    public ReturnData UpdateRecordCondition(@RequestBody Record record){
        returnData.setData(null);
        Integer ok=recordService.updateRecordCondition(record);
        if(ok==1){
            returnData.setStatus(true);
            returnData.setMsg("成功");
        }else{
            returnData.setStatus(false);
            returnData.setMsg("失败");
        }

        return returnData;
    }

    @PostMapping("DelRecordList")
    public ReturnData DelRecordList(@RequestBody Map<String,Object> map){
        returnData.setData(null);
        Integer ok=recordService.delRecord((Integer) map.get("recordId"));
        if(ok==1){
            returnData.setStatus(true);
            returnData.setMsg("退出任务成功");
        }else{
            returnData.setStatus(false);
            returnData.setMsg("退出任务失败,已逾期或基本完成");
        }

        return returnData;
    }


    @PostMapping("getVal")
    public ReturnData getVal(@RequestBody Map<String,Object> map){

        returnData.setMsg("查询完毕");
        returnData.setStatus(true);
        returnData.setData(recordService.getChartVal(user.getUserId()));
        return returnData;
    }



}
